package com.example.es.repository;

import com.example.es.entity.Promotion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface PromotionRepository extends JpaRepository<Promotion, Long> {
    
    List<Promotion> findByActiveTrueAndStartTimeBeforeAndEndTimeAfter(LocalDateTime startTime, LocalDateTime endTime);
    
    List<Promotion> findByTypeAndActiveTrue(String type);
    
    List<Promotion> findByDiscountRateGreaterThanEqualAndActiveTrue(Double minDiscountRate);
    
    @Query("SELECT p FROM Promotion p WHERE p.active = true AND " +
           "(:name IS NULL OR p.name LIKE %:name%) AND " +
           "(:type IS NULL OR p.type = :type) AND " +
           "(:minDiscount IS NULL OR p.discountRate >= :minDiscount) AND " +
           "(:tags IS NULL OR p.tags LIKE %:tags%)")
    List<Promotion> searchPromotions(@Param("name") String name,
                                   @Param("type") String type,
                                   @Param("minDiscount") Double minDiscount,
                                   @Param("tags") String tags);
    
    List<Promotion> findByActiveTrueOrderByCreatedAtDesc();
    
    // JpaRepository already provides these methods:
    // Optional<Promotion> findById(Long id);
    // Promotion save(Promotion entity);
    // void deleteById(Long id);
}